package com.tc.utils.image.table;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Frederic.Tang on 2018/8/23.
 */
public class Table {
    List<TableRow> rows = new ArrayList<>();
    int rh;//row height
    int col;
    int[] colw;
    int x, y;
    int marginx = 100;
    int marginy = 50;
    int fontSize = 20;
    static int padx = 20;
    static int pady = 10;

    int w,h;
    public Table(int col) {
        if (col < 1) {
            col = 1;
        }
        this.col = col;
        colw = new int[col];
    }

    public TableRow appendRow() {
        TableRow item = new TableRow(col);
        this.rows.add(item);
        return item;
    }

    public TableRow appendRow(int col) {
        TableRow item = new TableRow(col);
        this.rows.add(item);
        return item;
    }

    public void calc(Graphics2D g) {

        Font font = new Font("黑体", Font.PLAIN, fontSize);
        g.setFont(font);

        FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
        Rectangle rec = font.getStringBounds("a", frc).getBounds();

        //FontDesignMetrics metrics = FontDesignMetrics.getMetrics(g.getFont());

        rh = (int) (rec.getHeight() + 2 * pady);
        System.out.println("calc");

        x = marginx;
        y = marginy;
        //long time = System.currentTimeMillis();
        for (TableRow row : rows) {
            row.calc(g);
        }
        //System.out.println("time=" + (System.currentTimeMillis() - time) / 1000.0);


        for (int r = 0; r < rows.size(); r++) {
            for (int c = 0; c < col; c++) {
                int cw = rows.get(r).cellw[c];
                if (cw > colw[c]) {
                    colw[c] = cw;
                }
            }
        }
        w=0;
        for (int c = 0; c < col; c++) {
            w+=colw[c];
            //System.out.println("colw["+c+"]="+colw[c]);

        }
        w+=2*marginx;
        h=rows.size()*rh+2*marginy;
    }

    public void draw(Graphics2D g) throws Exception {

        Font font = new Font("黑体", Font.PLAIN, fontSize);
        g.setFont(font);

        System.out.println(rh);
        g.setColor(Color.black);


        int offsety = y;

        for (int r = 0; r < rows.size(); r++) {
            TableRow row = rows.get(r);

            int offsetx = x;
            for (int c = 0; c < col; c++) {

                String text = row.getCellText(c);
                g.drawString(text, offsetx+padx+(colw[c]-row.cellw[c])/2, offsety + rh * 9 / 10 - pady);
                g.drawRect(offsetx, offsety, colw[c], rh);
                System.out.println(x + "," + y);

                offsetx += colw[c];
            }
            offsety += rh;
        }
    }



    public void write( String file) throws Exception {
        Table table=this;
        long time = System.currentTimeMillis();

        BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();


        table.calc(g);


        image = new BufferedImage(table.w, table.h, BufferedImage.TYPE_INT_RGB);
        g = image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


        GradientPaint paint = new GradientPaint(10, 10, new Color(237, 238, 241), 10, 15, new Color(241, 242, 244), true);
        g.setPaint(paint);
        g.fillRect(0, 0, table.w, table.h);
        table.draw(g);
        g.dispose();
        System.out.println("time=" + (System.currentTimeMillis() - time) / 1000.0);

        OutputStream os = new FileOutputStream(file);

        ImageIO.write(image, "JPEG", os);


        System.out.println("time=" + (System.currentTimeMillis() - time) / 1000.0);

    }
}
